/*
 * Copyright 2011 The Rusted Roof, LLC.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.proteusframework.sample.plugin.registrar;

import org.proteusframework.api.base.AbstractPluginRegistrar;
import org.proteusframework.api.model.DisplayableName;
import org.proteusframework.api.model.IDisplayableName;
import org.proteusframework.sample.plugin.spi.SamplePluginFactory;

import java.util.ServiceLoader;
import java.util.logging.Logger;

/**
 * @author Tacoma Four
 */
public class PluginRegistrar extends AbstractPluginRegistrar
{
    private static final Logger logger = Logger.getLogger(PluginRegistrar.class.getCanonicalName());

    public static final IDisplayableName DN = new DisplayableName("sample",
            "PluginRegistrar",
            "Sample Proteus Plugin Registrar",
            "Registrar for all ISample factory plugins");

    public PluginRegistrar()
    {
        super(DN, VersionCollection.getInstance());

        ServiceLoader<SamplePluginFactory> loader = ServiceLoader.load(SamplePluginFactory.class);
        for (SamplePluginFactory pluginFactory : loader)
        {
            String displayName = pluginFactory.getDisplayableName().toString();
            logger.info("Registering plugin factory " + displayName + " with " + PluginRegistrar.class.getName() + ".");

            registrar.put(pluginFactory.getDisplayableName(), pluginFactory);

            VersionCollection.getInstance().registerVersion(pluginFactory.getDisplayableName(),
                    pluginFactory.supportedVersions());
        }
    }
}


